#include "CreditsState.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "MainMenuState.h"
#include "Game.h"
#include "BitmapFont.h"
#include <fstream>

CreditsState* CreditsState::GetInstance()
{
	static CreditsState s_Instance;

	return &s_Instance;
}

void CreditsState::Enter()
{
	
	m_nNumItems = 0;

	std::fstream file;
	file.open("resource/menu/Credits.mnu", std::ios_base::in);

	if (file.is_open())
	{
		file >> m_nNumItems;
		file.ignore(INT_MAX, '\n');
		for (unsigned char i = 0; i < m_nNumItems; i++)
		{
			char szTemp[100];
			file.getline(&szTemp[0], 100, '\n');

			SGD::String strItem;
			for (unsigned char i = 0; i < 100; i++)
			{
				strItem += szTemp[i];
				if (szTemp[i] == '\0')
				{
					strItem += szTemp[i];
					break;
				}
			}
			m_szItems.push_back(strItem);
		}
	}
	file.close();
}

void CreditsState::Exit()
{

}

void CreditsState::Update(float dt)
{

}

void CreditsState::Render()
{
	BitmapFont temp;
	temp.Initialize();
	Game * game = Game::GetInstance();

	for (unsigned int i = 0; i < m_nNumItems; i++)
		temp.Draw(&m_szItems[i],  32 * 2, 100+ i * 60, 0.55f, SGD::Color());
}

bool CreditsState::Input()
{
	if (SGD::InputManager::GetInstance()->IsKeyPressed(SGD::Key::Escape)
		|| SGD::InputManager::GetInstance()->IsButtonDown(0,1))
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());

	return true;
}